home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / flying-6.11 / snooker.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-30  |  5.4 KB  |  233 lines

  1. #ifndef _global_h
  2. #    include "global.h"
  3. #endif
  4.  
  5. #ifndef _snooker_h
  6. #    include "snooker.h"
  7. #endif
  8. #ifndef _pocket_
  9. #    include "pocket.h"
  10. #endif
  11. #ifndef _graph_h
  12. #    include "graph.h"
  13. #endif
  14. #ifndef _mover_h
  15. #    include "mover.h"
  16. #endif
  17.  
  18.  
  19. //
  20. // Voreinstellungen
  21. //
  22.  
  23.  
  24. Snooker::Snooker(double wx, double wy) :
  25. Pool(wx,wy)
  26. {
  27.     InitArea( TableWidth, TableHeight );
  28.  
  29.     SelectTable(-1);
  30.  
  31. #ifndef __TURBOC__
  32.     red_col = AddBallColor( "red3" );
  33.     cols[0] = AddBallColor( "yellow1" );
  34.     cols[1] = AddBallColor( "green4" );
  35.     cols[2] = AddBallColor( "brown4" );
  36.     cols[3] = AddBallColor( "blue" );
  37.     cols[4] = AddBallColor( "HotPink3" );
  38.     cols[5] = AddBallColor( "black" );
  39. #else
  40.     red_col = AddBallColor( "red3" );
  41.     floor   = CreateColorMix( table_col, table_line_col );
  42.  
  43.     cols[5] = AddBallColor( "black" );
  44.     cols[0] = AddBallColor( "yellow1" );
  45.     cols[1] = AddBallColor( "green4" );
  46.     cols[2] = CreateColorMix( red_col, cols[5] );        // braun
  47.     cols[3] = AddBallColor( "blue" );
  48.     cols[4] = CreateColorMix( red_col, cue_col );    // Pink
  49. #endif
  50.  
  51.     cueball=0;
  52.     color_in_pocket = 0;
  53.     reds_in_pocket  = 0;
  54. }
  55.  
  56.  
  57. Snooker::~Snooker() {
  58.     if (cueball) {
  59.         delete cueball;
  60.         for (int c=0;c<6;c++)        delete colored[c];
  61.         for (int r=0;r<15;r++)        delete red[r];
  62.         cueball=0;
  63.     }
  64. }
  65.  
  66. const Real & Snooker::GetNormalBallSize() const {
  67.     return BallRadius;
  68. }
  69.  
  70.  
  71. void Snooker::Triangle( double x, double y )
  72. {
  73. const int count = 5;
  74. int        c=0;
  75. double    cdist = sqrt( 3.*(GetNormalBallSize()+Offset)*(GetNormalBallSize()+Offset) );
  76.  
  77. //    x-=(cdist*(count-1)/2);
  78.     for( int col=0; col<count; col++ ) {
  79.         for( int row=0; row<=col; row++ ) {
  80.             red[c]=new Ball( x+col*cdist, y+(row-col/2.0)*2*(GetNormalBallSize()+Offset), 0, 0, GetNormalBallSize() );
  81.             red[c]->state = new BallState( m, red_col, red[c]->P() );
  82.             c++;
  83.         }
  84.     }
  85. }
  86.  
  87. void Snooker::SetupBalls() {
  88.     defs[0] = Vec2( AreaOffX()+AreaWidth()*0.25,  AreaOffY()+AreaHeight()/2.+14.6 );
  89.     defs[1] = Vec2( AreaOffX()+AreaWidth()*0.25,  AreaOffY()+AreaHeight()/2.-14.6 );
  90.     defs[2] = Vec2( AreaOffX()+AreaWidth()*0.25,  AreaOffY()+AreaHeight()*0.50 );
  91.     defs[3] = Vec2( AreaOffX()+AreaWidth()*0.50,  AreaOffY()+AreaHeight()*0.50 );
  92.     defs[4] = Vec2( AreaOffX()+AreaWidth()*0.75,  AreaOffY()+AreaHeight()*0.50 );
  93.     defs[5] = Vec2( AreaOffX()+AreaWidth()-32.0, AreaOffY()+AreaHeight()*0.50 );
  94.  
  95.     for (int i=0;i<6;i++) {
  96.         colored[i] = new Ball( defs[i].X(), defs[i].Y(), 0, 0, BallRadius );
  97.         colored[i]->state = new BallState( m, cols[i], colored[i]->P() );
  98.     }
  99.  
  100.     cuedef  = Vec2( AreaOffX()+AreaWidth()*0.125, AreaOffY()+AreaHeight()*0.375 );
  101.     cueball = new Ball( cuedef.X(), cuedef.Y(),
  102.                                 RealZero, RealZero, BallRadius );
  103.     cueball->state = new BallState( m, cue_col, cueball->P() );
  104.  
  105.     tridef  = Vec2(    AreaOffX()+AreaWidth()*0.75+GetNormalBallSize()*2.5,
  106.                             AreaOffY()+AreaHeight()/2. );
  107.     Triangle(tridef);
  108. }
  109.  
  110. void Snooker::InitPlayground() {
  111.     Billard::InitPlayground();
  112.     BallRadius = m->GetActRadius();
  113.     InitTable(6.0);
  114.     SetupBalls();
  115. }
  116.  
  117. void Snooker::DrawBackground() const {
  118.     Pool::DrawBackground();
  119.     SetBgColor(table_line_col);
  120.     DrawLine( defs[0], defs[1] );
  121.     DrawArc( defs[2], 14.6, 90.0, 180.0 );
  122.     for (int i=0;i<6;i++)     FillCircle( defs[i], 1.0 );
  123. }
  124.  
  125.  
  126. // -------------------------------------------------------------------------
  127.  
  128. SnookerDemo::~SnookerDemo() {}
  129.  
  130. const Real &SnookerDemo::GetPresetA() const            { return PresetA; }
  131. const Real &SnookerDemo::GetSlowGranularity() const    { return SlowGranularity; }
  132.  
  133. void SnookerDemo::InitPlayground() {
  134.     Billard::InitPlayground();
  135.     BallRadius = m->GetActRadius();
  136.     Billard::InitTable();
  137.     SetupBalls();
  138.     cueball->v = Vec2( shot_speed, shot_speed );
  139. }
  140.  
  141. void SnookerDemo::DrawBackground() const {
  142.     Billard::DrawBackground();
  143.     SetBgColor(table_line_col);
  144.     DrawLine( defs[0], defs[1] );
  145.     DrawArc( defs[2], 14.6, 90.0, 180.0 );
  146.     for (int i=0;i<6;i++)     FillCircle( defs[i], 1.0 );
  147. }
  148.  
  149. ////////////////////////////////////////////////////////////////////////////
  150.  
  151. void Snooker::InPocket( Ball *b ) {
  152.     for (int i=0;i<6;i++) {
  153.         if (b==colored[i]) {
  154.             color_in_pocket |= (1<<i);
  155.             return;
  156.         }
  157.     }
  158.     for (i=0;i<15;i++) {
  159.         if (b==red[i]) {
  160.             reds_in_pocket  |= (1<<i);
  161.             return;
  162.         }
  163.     }
  164.     Pool::InPocket(b);
  165. }
  166.  
  167. void Snooker::AllBallsStopped() {
  168.     if (color_in_pocket) {
  169.         int    mask=0;
  170.         for (int i=0;i<6;i++) {
  171.             if ((reds_in_pocket!=0x7fff)||((color_in_pocket&mask)!=mask)) {
  172.                 if (color_in_pocket & (1<<i)) {
  173.                     (void)IsSelectable(colored[i]);
  174.                 }
  175.             }
  176.             mask = (mask<<1)|1;
  177.         }
  178.     }
  179.     Pool::AllBallsStopped();
  180. }
  181.  
  182. int Snooker::IsSelectable(Ball *b) {
  183.     for (int i=0;i<6;i++) {
  184.         if (b==colored[i]) {
  185.             if (color_in_pocket & (1<<i)) {
  186.                 if (b->FitsAt(defs[i])) {
  187.                     b->SetP(defs[i]);        // reset to default position
  188.                 }
  189.                 else if (b->FitsAt(defs[5])) {
  190.                     b->SetP(defs[5]);        // reset to black position
  191.                 }
  192.                 else if (i<4) {
  193.                     Vec2    newpos;
  194.                     b->FitsNextTo(defs[i],Vec2(-1.0,RealZero),&newpos);
  195.                     b->SetP(newpos);        // closer to left wall
  196.                 }
  197.                 else {
  198.                     Vec2    newpos;
  199.                     b->FitsNextTo(defs[i],Vec2(1.0,RealZero),&newpos);
  200.                     b->SetP(newpos);        // closer to right wall
  201.                 }
  202.  
  203.                 b->ChgV(Vec2Zero);
  204.                 color_in_pocket &= ~(1<<i);        // it's back in the game
  205.  
  206.                 return 0;
  207.             }
  208.             else {
  209.                 return 1;
  210.             }
  211.         }
  212.     }
  213.     for (i=0;i<15;i++) {
  214.         if (b==red[i]) {
  215.             if (reds_in_pocket & (1<<i)) {
  216.                 Vec2    newpos;
  217.                 b->FitsNextTo(tridef,Vec2(1.0,RealZero),&newpos);
  218.                 b->SetP(newpos);                    // closer to right wall
  219.  
  220.                 b->ChgV(Vec2Zero);
  221.                 reds_in_pocket &= ~(1<<i);        // it's back in the game
  222.  
  223.                 return 0;
  224.             }
  225.             else {
  226.                 return 1;
  227.             }
  228.         }
  229.     }
  230.  
  231.     return Pool::IsSelectable(b);
  232. }
  233.